package bairui.international.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bairui.international.model.domain.jpa.TB_BranchCompany;
import bairui.international.model.domain.jpa.TB_BranchCompanyMoneyRecord;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_BranchCompanyWithdrawOrder;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.type.Type_BranchCompanyMoneyRecord;
import bairui.international.model.type.Type_ProductOrderPay;
import bairui.international.service.Service_BranchCompany;
import bairui.international.service.Service_BranchCompanyMoneyRecord;
import bairui.international.service.Service_BranchCompanyWithdrawOrder;
import bairui.international.service.Service_ProductOrder;

/**
 * 
 * 【(分公司金额记录)TB_BranchCompanyMoneyRecord】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_BranchCompanyMoneyRecord 模型
 * @see Service_BranchCompanyMoneyRecord 服务接口
 * @see BService_BranchCompanyMoneyRecordImpl 服务实现基础
 *
 */
@Service
public class Service_BranchCompanyMoneyRecordImpl extends BService_BranchCompanyMoneyRecordImpl implements Service_BranchCompanyMoneyRecord {


	@Autowired private Service_BranchCompanyWithdrawOrder service_BranchCompanyWithdrawOrder;
	@Autowired private Service_BranchCompany service_BranchCompany;
	@Autowired private Service_ProductOrder service_ProductOrder;
	
	private TB_BranchCompanyMoneyRecord createInstance(
		Double money, TB_BranchCompany branchCompany, TB_BranchCompanyStaff branchCompanyStaff, Type_BranchCompanyMoneyRecord type,
		TB_BranchCompanyWithdrawOrder userWithdrawOrder, TB_ProductOrder order,
		boolean flag
	) {
		// 设置总金额
		TB_BranchCompany userAmount = service_BranchCompany.findByIdUseLock(branchCompany.getId());
		double currentMoney = BigDecimal
				.valueOf(userAmount.getMoney())
				.add(BigDecimal.valueOf(money))
				.doubleValue();
		userAmount.setMoney(currentMoney);
		//
		double _currentWithdrawMoney = userAmount.getWithdrawMoney();
		if(flag){
			_currentWithdrawMoney = BigDecimal
					.valueOf(_currentWithdrawMoney)
					.add(BigDecimal.valueOf(money))
					.doubleValue();
			userAmount.setWithdrawMoney(_currentWithdrawMoney);
		}
		double currentWithdrawMoney = _currentWithdrawMoney;
		//
		return dao.save(new TB_BranchCompanyMoneyRecord(
				new TB_BranchCompanyMoneyRecord.MustProperties() {
					@Override public Double getMoney() {
						return money;
					}
					@Override public Double getCurrentMoney() {
						return currentMoney;
					}
					@Override public Type_BranchCompanyMoneyRecord getType() {
						return type;
					}
					@Override public TB_BranchCompany getBranchCompany() {
						return branchCompany;
					}
					@Override public Double getCurrentWithdrawMoney() {
						return currentWithdrawMoney;
					}
				},
				new TB_BranchCompanyMoneyRecord.OptionalSingleProperties() {
					@Override public TB_BranchCompanyWithdrawOrder getBranchCompanyWithdrawOrder() {
						return userWithdrawOrder;
					}
					@Override public TB_ProductOrder getProductOrder() {
						return order;
					}
					@Override public TB_BranchCompanyStaff getBranchCompanyStaff() {
						return branchCompanyStaff;
					}
				}
			));
	}

	/**
	 * 提现
	 */
	@Override
	public TB_BranchCompanyMoneyRecord create(TB_BranchCompanyWithdrawOrder userWithdrawOrder) {
		userWithdrawOrder = service_BranchCompanyWithdrawOrder.load(userWithdrawOrder);
		//
		return createInstance(
				-userWithdrawOrder.getMoney(), 
				userWithdrawOrder.getBranchCompany(), 
				userWithdrawOrder.getBranchCompanyStaff(),
				Type_BranchCompanyMoneyRecord.Withdraw, 
				userWithdrawOrder,
				null,
				true
			);
	}
	
	/**
	 * 提现退回
	 */
	@Override
	public TB_BranchCompanyMoneyRecord createReturn(TB_BranchCompanyWithdrawOrder userWithdrawOrder) {
		userWithdrawOrder = service_BranchCompanyWithdrawOrder.load(userWithdrawOrder);
		//
		return createInstance(
				userWithdrawOrder.getMoney(), 
				userWithdrawOrder.getBranchCompany(), 
				userWithdrawOrder.getBranchCompanyStaff(),
				Type_BranchCompanyMoneyRecord.WithdrawReturn, 
				userWithdrawOrder,
				null,
				true
			);
	}
	
	/**
	 * 来自订单
	 */
	@Override
	public TB_BranchCompanyMoneyRecord create(TB_ProductOrder order) {
		order = service_ProductOrder.load(order);
		boolean flag = false;
		if(order.getPayWay() == Type_ProductOrderPay.OnLine){
			flag = true;
		}
		//
		return createInstance(
				order.getPay(), 
				order.getBranchCompany(), 
				order.getAssignor(),
				Type_BranchCompanyMoneyRecord.Order, 
				null,
				order,
				flag
			);
	}

}
